package telas;
import java.awt.event.ActionEvent;

import cliente.Cliente;

import com.cloudgarden.layout.AnchorConstraint;
import com.cloudgarden.layout.AnchorLayout;

import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.JTree;

import javax.swing.WindowConstants;
import javax.swing.SwingUtilities;


/**
 * This code was edited or generated using CloudGarden's Jigloo
 * SWT/Swing GUI Builder, which is free for non-commercial
 * use. If Jigloo is being used commercially (ie, by a corporation,
 * company or business for any purpose whatever) then you
 * should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details.
 * Use of Jigloo implies acceptance of these licensing terms.
 * A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
 * THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
 * LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class tela_cadastro extends javax.swing.JFrame {
	private JLabel cadastro_label;
	private JLabel nome_label;
	private JTextField usuario_textfield;
	private JLabel usuario_label;
	private JTextField foto_textfield;
	private JLabel foto_jlabel;
	private JButton voltar_botao;
	private JButton cadastro_botao;
	private JButton foto_botao;
	private JLabel foto_label;
	private JPasswordField senha_textfield;
	private JLabel senha_label;
	private JTextField nome_textfield;
	public String[] args;
	private ComboBoxModel combo_fotosModel;
	private JComboBox combo_fotos;
	private ImageIcon[] images;
	/**
	 * Auto-generated main method to display this JFrame
	 */
	/*
	public static void main(String[] args) {

		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				tela_cadastro inst = new tela_cadastro();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}*/
	public tela_cadastro(String[] args) {
		super();
		this.args = args;
		initGUI();
	}

	private void initGUI() {

		try {
			AnchorLayout thisLayout = new AnchorLayout();
			getContentPane().setLayout(thisLayout);
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			{
				voltar_botao = new JButton();
				getContentPane().add(voltar_botao, new AnchorConstraint(889, 743, 972, 495, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				voltar_botao.setText("Voltar");
				voltar_botao.setPreferredSize(new java.awt.Dimension(105, 26));
				//Evento que trata a volta a tela de login
				voltar_botao.addActionListener(new java.awt.event.ActionListener() {  
					public void actionPerformed(java.awt.event.ActionEvent evt) {
						//Método que trata a volta a tela de login
						botaoVoltarParaLogin(evt);  
					} 
				});
			}
			{
				cadastro_botao = new JButton();
				getContentPane().add(cadastro_botao, new AnchorConstraint(889, 469, 972, 211, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				cadastro_botao.setText("Cadastrar");
				cadastro_botao.setPreferredSize(new java.awt.Dimension(109, 26));
				//Método que trata o cadastramento de um usuário
				cadastro_botao.addActionListener(new java.awt.event.ActionListener() {  
					public void actionPerformed(java.awt.event.ActionEvent evt) {
						//Método que trata o cadastramento de um usuário
						botaoCadastrarUsuario(evt);
					}
				});
			}
			{
				//Mostra o caminho da imagem
				//System.out.println(ClassLoader.getSystemResource("").getPath()+"../imagens/sem_foto.png");
				//Cria um ImageIcon para passar ao servidor a imagem 
				//ImageIcon img = new ImageIcon("foto/sem_foto.png");
				//foto_jlabel = new JLabel(img);
				//getContentPane().add(foto_jlabel, new AnchorConstraint(564, 592, 856, 365, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				//foto_jlabel.setPreferredSize(new java.awt.Dimension(96, 91));
			}
			{
				cadastro_label = new JLabel();
				getContentPane().add(cadastro_label, new AnchorConstraint(40, 672, 91, 405, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				cadastro_label.setText("Cadastro");
				cadastro_label.setPreferredSize(new java.awt.Dimension(113, 16));
			}
			{
				nome_label = new JLabel();
				getContentPane().add(nome_label, new AnchorConstraint(152, 183, 204, 41, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				nome_label.setText("Nome:");
				nome_label.setPreferredSize(new java.awt.Dimension(60, 16));
			}
			{
				//campo do nome do usuário
				nome_textfield = new JTextField();
				getContentPane().add(nome_textfield, new AnchorConstraint(143, 887, 217, 183, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				nome_textfield.setPreferredSize(new java.awt.Dimension(298, 23));
			}
			{
				//Campo do login do usuário
				usuario_textfield = new JTextField();
				getContentPane().add(usuario_textfield, new AnchorConstraint(249, 556, 323, 183, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				usuario_textfield.setPreferredSize(new java.awt.Dimension(158, 23));
			}
			{
				usuario_label = new JLabel();
				getContentPane().add(usuario_label, new AnchorConstraint(258, 183, 310, 41, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				usuario_label.setText("Usuário:");
				usuario_label.setPreferredSize(new java.awt.Dimension(60, 16));
			}
			{
				senha_label = new JLabel();
				getContentPane().add(senha_label, new AnchorConstraint(361, 183, 413, 41, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				senha_label.setText("Senha:");
				senha_label.setPreferredSize(new java.awt.Dimension(60, 16));
			}
			{
				//Campo do password do usuário
				senha_textfield = new JPasswordField();
				getContentPane().add(senha_textfield, new AnchorConstraint(352, 556, 426, 183, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				senha_textfield.setPreferredSize(new java.awt.Dimension(158, 23));
			}
			{
				
				String[] avatarStrings = {"user0", "user1", "user2", "user3", "user4", "user5", "user6", "user7", "user8", "user9", "user10", "user11", "user12", "user13", "user14", "user15" };
				
				images = new ImageIcon[avatarStrings.length];
		        Integer[] intArray = new Integer[avatarStrings.length];
		        for (int i = 0; i < avatarStrings.length; i++) {
		            intArray[i] = new Integer(i);
		            images[i] = new ImageIcon("foto/" + avatarStrings[i] + ".png");
		            if (images[i] != null) {
		                images[i].setDescription(avatarStrings[i]);
		            }
		        }
				
		        //JComboBox petList = new JComboBox(intArray);
		        
				combo_fotosModel = new DefaultComboBoxModel( images );
				combo_fotos = new JComboBox();
				getContentPane().add(combo_fotos, new AnchorConstraint(503, 644, 795, 289, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				combo_fotos.setModel(combo_fotosModel);
				combo_fotos.setPreferredSize(new java.awt.Dimension(150, 91));
			}
			{
				//foto_label = new JLabel();
				//getContentPane().add(foto_label, new AnchorConstraint(474, 183, 525, 41, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				//foto_label.setText("Foto:");
				//foto_label.setPreferredSize(new java.awt.Dimension(60, 16));
			}
			{
				//foto_textfield = new JTextField();
				//getContentPane().add(foto_textfield, new AnchorConstraint(464, 762, 538, 183, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				//foto_textfield.setPreferredSize(new java.awt.Dimension(245, 23));
			}
			{
				//foto_botao = new JButton();
				//getContentPane().add(foto_botao, new AnchorConstraint(464, 975, 538, 776, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				//foto_botao.setText("Buscar");
				//foto_botao.setPreferredSize(new java.awt.Dimension(84, 23));
			}
			pack();
			this.setSize(439, 349);
		} catch (Exception e) {
			//add your error handling code here
			e.printStackTrace();
		}
	}
	
	//Método que trata o cadastramento de um usuário
	public void botaoCadastrarUsuario(ActionEvent evt) {
		
		//System.out.println("TESTEEEE:");
		//System.out.println(combo_fotos.getSelectedItem());
		
		
		//Senha 
		String senha_usuario = new String(senha_textfield.getPassword());

		//Validação do nome do usuário
		if( nome_textfield.getText().matches("[a-zA-Z]+") == false ) {
			JOptionPane.showMessageDialog(null, "Campo Nome só deve conter letras e sem acento! (Campo também não pode estar vazio!)", "Nome", JOptionPane.ERROR_MESSAGE); 
		}//Validação do usuário 
		else if( usuario_textfield.getText().matches("[a-zA-Z]+") == false ) {
			JOptionPane.showMessageDialog(null, "Campo Usuário só deve conter letras e sem acento! (Campo também não pode estar vazio!)", "Usuário", JOptionPane.ERROR_MESSAGE); 
		}//Validação da senha do usuário 
		else if( new String(senha_textfield.getPassword()).matches("[a-zA-Z0-9]+") == false ) {
			JOptionPane.showMessageDialog(null, "Senha deve conter somente letras(sem acento) e números! (Campo também não pode estar vazio!)", "Nome de Usuário", JOptionPane.ERROR_MESSAGE);
		} else {
			//Cria um novo clientes para realizar o cadastro no servidor
			Cliente cliente = new Cliente( this.args, nome_textfield.getText(), usuario_textfield.getText(), senha_usuario, 0.0, 0.0, true, "offline",combo_fotos.getSelectedItem().toString());

			//Ao cadastrar voltar para tela de login:

			//Destroi a tela atual
			this.dispose(); 

			//Nova tela de login
			tela_login inst = new tela_login(args);
			//Centraliza a janela
			inst.setLocationRelativeTo(null);
			inst.setVisible(true);
		}

	}

	//Método que trata a volta a tela de login
	public void botaoVoltarParaLogin(ActionEvent evt) {

		//Fecha a tela atual
		this.dispose(); 

		//Cria uma nova tela de login
		tela_login inst = new tela_login(args);
		inst.setLocationRelativeTo(null);
		inst.setVisible(true);

	}


}
